# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => '8acf66d6287d43d08a4247c5e37bb320'
  
  before_filter :check_callback_daemon
  
private
  def check_callback_daemon
    start_daemon if not daemon_running?
    
    if not daemon_running?
      raise 'Can not start mpd_callback daemon'
    end
  end
  
  def start_daemon
    system "#{RAILS_ROOT}/script/daemons start"
  end
  
  def daemon_running?
    pidfile = Daemons::PidFile.existing("#{RAILS_ROOT}log/mpd_callbacks.rb.pid")
    pid = pidfile.pid
    Daemons::Pid.running? pid
  end
end
